package com.palfund.nested;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;

public class AppBarRVActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_bar_rv);
        Toolbar toolbar = findViewById(R.id.toolBar);
        setSupportActionBar(toolbar);
        setTitle("AppBarLayout");
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
        int[] pictureIds = {R.drawable.p6, R.drawable.p7, R.drawable.p8, R.drawable.p9, R
                .drawable.p10, R.drawable.p11, R.drawable.p12, R.drawable.p13};
        ArrayList<ImageView> imageViews = new ArrayList<>();
        ViewPager.LayoutParams layoutParams = new ViewPager.LayoutParams();
        layoutParams.width = ViewPager.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewPager.LayoutParams.MATCH_PARENT;
        for (int i = 0; i < pictureIds.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setImageResource(pictureIds[i]);
            imageViews.add(imageView);
        }
        VPAdapter viewPagerAdapter = new VPAdapter(imageViews);
        viewPager.setAdapter(viewPagerAdapter);
        RecyclerView recyclerViewTop = (RecyclerView) findViewById(R.id.recyclerViewTop);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2, GridLayoutManager
                .HORIZONTAL, false);
        recyclerViewTop.setLayoutManager(gridLayoutManager);
        RVAdapter rvAdapterTop = new RVAdapter(this);
        rvAdapterTop.orientation = 1;
        recyclerViewTop.setAdapter(rvAdapterTop);
        ArrayList<String> dataTop = new ArrayList<>();
        for (int i = 0; i < 16; i++) {
            dataTop.add("top   " + i);
        }
        rvAdapterTop.addAll(dataTop, false);
        RecyclerView recyclerViewBottom = (RecyclerView) findViewById(R.id.recyclerViewBottom);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerViewBottom.setLayoutManager(linearLayoutManager);
        recyclerViewBottom.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int first = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
                int last = linearLayoutManager.findLastCompletelyVisibleItemPosition();
                if (first == 0) {
                    Toast.makeText(AppBarRVActivity.this, "Top", Toast.LENGTH_SHORT).show();
                }
                if (last == linearLayoutManager.getItemCount() - 1) {
                    Toast.makeText(AppBarRVActivity.this, "Bottom", Toast.LENGTH_SHORT).show();
                }
                Log.i("ScrollingActivity", "---onScrolled()--->" + dy);
            }
        });

        ArrayList<ArrayList<String>> rrData = new ArrayList<>();
        for (char i = 'A'; i < 'Z'; i++) {
            ArrayList<String> data = new ArrayList<>();
            for (int j = 0; j < 66; j++) {
                data.add(i + "   " + j);
            }
            rrData.add(data);
        }

        RRAdapter rrAdapter = new RRAdapter(this);
        recyclerViewBottom.setAdapter(rrAdapter);
        rrAdapter.addAll(rrData);
    }
}
